/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.foxline.clinicplus.manager.search;

import br.com.foxline.clinicplus.entity.Cash;
import br.com.foxline.clinicplus.entity.Income;
import br.com.foxline.clinicplus.enums.TypeCredit;
import br.com.foxline.clinicplus.enums.TypeDebt;
import br.com.foxline.clinicplus.enums.TypeIncome;
import br.com.foxline.clinicplus.service.CashService;
import br.com.foxline.clinicplus.service.DoctorService;
import br.com.foxline.clinicplus.service.IncomeService;
import br.com.foxline.clinicplus.service.ProcedureService;
import br.com.foxline.clinicplus.service.SystemUserService;
import br.com.foxline.clinicplus.util.Msg;
import java.io.Serializable;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.*;
import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.model.SelectItem;

/**
 *
 * @author roniere
 */
@ManagedBean
@ViewScoped
public class ManagerSearchReceita implements Serializable {

    @EJB
    IncomeService incomeService;
    @EJB
    DoctorService doctorService;
    @EJB
    ProcedureService procedureService;
    @EJB
    SystemUserService systemUserService;
    @EJB
    CashService cashService;
    private Income income;
    private Income incomeeditar;
    private List<Income> incomes;
    private BigDecimal total;
    private Income incomeremover;
    private Date dateInitial;
    private Date dateFinal;

    @PostConstruct
    public void init() {
        income = new Income();
        total = new BigDecimal(BigInteger.ZERO);
        incomeeditar = new Income();
    }

    public List<SelectItem> selectTypeOutput() {
        List<SelectItem> list = new ArrayList<SelectItem>();

        for (TypeCredit ms : TypeCredit.values()) {
            list.add(new SelectItem(ms, ms.getDesc()));
        }
        return list;
    }

    public Income getIncome() {
        return income;
    }

    public void setIncome(Income income) {
        this.income = income;
    }

    public List<Income> getIncomes() {
        return incomes;
    }

    public void setIncomes(List<Income> incomes) {
        this.incomes = incomes;
    }

    public BigDecimal getTotal() {
        return total;
    }

    public void setTotal(BigDecimal total) {
        this.total = total;
    }

    public Income getIncomeeditar() {
        return incomeeditar;
    }

    public void setIncomeeditar(Income incomeeditar) {
        this.incomeeditar = incomeeditar;
    }

    public void search() {
        Calendar gc = GregorianCalendar.getInstance();

        gc.setTime(dateInitial);
        gc.set(GregorianCalendar.HOUR_OF_DAY, 0);
        gc.set(GregorianCalendar.MINUTE, 0);
        gc.set(GregorianCalendar.SECOND, 0);
        gc.set(GregorianCalendar.MILLISECOND, 0);

        Calendar gc1 = GregorianCalendar.getInstance();
        gc1.set(GregorianCalendar.HOUR_OF_DAY, 23);
        gc1.set(GregorianCalendar.MINUTE, 0);
        gc1.set(GregorianCalendar.SECOND, 0);
        gc1.set(GregorianCalendar.MILLISECOND, 0);

        dateFinal = gc1.getTime();

        total = incomeService.getValueIncomeGeneric(null, null, null, dateInitial, dateFinal, null, null, TypeIncome.R, null, null, null, null, income.getTypeCredit(), null, null, null, null);
        incomes = incomeService.findIncomeGeneric(null, null, null, dateInitial, dateFinal, null, null, TypeIncome.R, null, null, null, null, income.getTypeCredit(), null, null, null, null);
    }

    public void loadEditar(int id) {
        incomeeditar = incomes.get(id);
    }

    public void loadRemover(int id) {
        incomeremover = incomes.get(id);
    }

    public void editar() {
        try {
            if (incomeeditar.getId() != null) {
                incomeeditar.setAmountPaid(incomeeditar.getTotalAmount());
                incomeService.update(incomeeditar);
                Msg.messagemInfo("Registro atualizado com Sucesso !");
            }
            incomeeditar = new Income();
            search();

        } catch (Exception e) {
            Msg.messagemError("Erro na operação !");
            e.printStackTrace();
        }
    }

    public Income getIncomeremover() {
        return incomeremover;
    }

    public void setIncomeremover(Income incomeremover) {
        this.incomeremover = incomeremover;
    }

    public void remover() {
        try {
            if (incomeremover.getId() != null) {
                incomeremover.setAtivo(false);
                //REMOVENDO CASH
                if (incomeremover.getCash() != null) {
                    Cash c = incomeremover.getCash();
                    c.setStatus(false);
                    cashService.update(c);
                }
                incomeService.update(incomeremover);
                Msg.messagemInfo("Registro excluído com Sucesso !");
            }
            incomeremover = new Income();
            search();
        } catch (Exception e) {
            Msg.messagemError("Erro na operação !");
            e.printStackTrace();
        }
    }

    public Date getDateInitial() {
        return dateInitial;
    }

    public void setDateInitial(Date dateInitial) {
        this.dateInitial = dateInitial;
    }
}
